package org.jeecg.flowable.controller.definition;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.enums.common.CommonStatusEnum;
import org.jeecg.flowable.entity.definition.BpmForm;
import org.jeecg.flowable.entity.definition.BpmUserGroup;
import org.jeecg.flowable.service.definition.BpmUserGroupService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/bpm/user-group")
@Validated
public class BpmUserGroupController {

    @Resource
    private BpmUserGroupService userGroupService;

    @PostMapping("/create")
    @AutoLog(value = "创建用户组")
    @ApiOperation(value="创建用户组", notes="创建用户组")
    public Result<?> createUserGroup(@Valid @RequestBody BpmUserGroup createReqVO) {
        userGroupService.save(createReqVO);
        return Result.ok(createReqVO.getId());
    }

    @PutMapping("/update")
    @AutoLog(value = "更新用户组")
    @ApiOperation(value="更新用户组", notes="更新用户组")
    public Result<?> updateUserGroup(@Valid @RequestBody BpmUserGroup updateReqVO) {
        return Result.ok(userGroupService.updateById(updateReqVO));
    }

    @DeleteMapping("/delete")
    @AutoLog(value = "删除用户组")
    @ApiOperation(value="删除用户组", notes="删除用户组")
    public Result<?> deleteUserGroup(@RequestParam("id") Long id) {
        return Result.ok(userGroupService.removeById(id));
    }

    @GetMapping("/get")
    @AutoLog(value = "获得用户组")
    @ApiOperation(value="获得用户组", notes="获得用户组")
    public Result<?> getUserGroup(@RequestParam("id") Long id) {
        BpmUserGroup userGroup = userGroupService.getById(id);
        return Result.ok(userGroup);
    }

    @GetMapping("/page")
    @AutoLog(value = "获得用户组分页")
    @ApiOperation(value="获得用户组分页", notes="获得用户组分页")
    public Result<?> queryPageList( BpmUserGroup bpmUserGroup,
                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                    HttpServletRequest req) {
        QueryWrapper<BpmUserGroup> queryWrapper = QueryGenerator.initQueryWrapper(bpmUserGroup, req.getParameterMap());
        Page<BpmUserGroup> page = new Page<BpmUserGroup>(pageNo, pageSize);
        IPage<BpmUserGroup> pageList = userGroupService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @GetMapping("/list-all-simple")
    @AutoLog(value = "获取用户组精简信息列表")
    @ApiOperation(value="获取用户组精简信息列表", notes="只包含被开启的用户组，主要用于前端的下拉选项")
    public Result<?> getSimpleUserGroups() {
        // 获用户门列表，只要开启状态的
        QueryWrapper<BpmUserGroup> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",CommonStatusEnum.ENABLE.getStatus());
        List<BpmUserGroup> list = userGroupService.list(queryWrapper);
        // 排序后，返回给前端
        return Result.ok(list);
    }
}
